/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datamodel;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import utils.DateUtils;

/**
 *
 * @author Kvest
 */
public class Message 
{
    private static final int DEFAULT_TIMEOUT = 10000;
    
    private String message;
    private String from;
    private String to;
    private String reciveDate;
    private boolean read;
    private int timeout;
    
    public Message(String message)
    {
        this.message = message;
        this.from = "";
        this.to = "";
        this.reciveDate = DateUtils.getFullDate();
        this.read = false;
        this.timeout = DEFAULT_TIMEOUT;
    }
    
    public Message(String message, String from, String to)
    {
        this.message = message;
        this.from = from;
        this.to = to;
        this.reciveDate = DateUtils.getFullDate();
        this.read = false;
        this.timeout = DEFAULT_TIMEOUT;
    }

    public Message(byte[] data) throws IOException
    {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DataInputStream dis = new DataInputStream(bais);
        
        try
        {
            message = dis.readUTF();
            from = dis.readUTF();
            to = dis.readUTF();
            reciveDate = dis.readUTF();
            read = dis.readBoolean();
            timeout = dis.readInt();
        }
        finally
        {
            try
            {
                dis.close();
                bais.close();
            }catch(Exception ex){}
        }  
    }
    
    public byte[] toByte()
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(result);
        
        try
        {
            dos.writeUTF(message);
            dos.writeUTF(from);
            dos.writeUTF(to);
            dos.writeUTF(reciveDate);
            dos.writeBoolean(read);
            dos.writeInt(timeout);
            
            return result.toByteArray();
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                dos.close();
                result.close();
            }catch(Exception ex){}
        }    
    }
    
    public String getFrom() 
    {
        return from;
    }

    public void setFrom(String from) 
    {
        this.from = from;
    }

    public String getMessage() 
    {
        return message;
    }

    public void setMessage(String message) 
    {
        this.message = message;
    }

    public String getReciveDate() 
    {
        return reciveDate;
    }

    public void setReciveDate(String reciveDate) 
    {
        this.reciveDate = reciveDate;
    }

    public String getTo() 
    {
        return to;
    }

    public void setTo(String to)
    {
        this.to = to;
    }

    public boolean isRead() 
    {
        return read;
    }

    public void setRead(boolean read) 
    {
        this.read = read;
    }

    public int getTimeout() 
    {
        return timeout;
    }

    public void setTimeout(int timeout) 
    {
        this.timeout = timeout;
    }
    
    public String toString()
    {
        return message;
    }
}
